package com.weds.data_sync.controller;

import com.weds.data_sync.pojo.dto.PageDto;
import com.weds.data_sync.pojo.dto.ScriptPlanDto;
import com.weds.data_sync.pojo.entity.DataSyncScriptPlanDO;
import com.weds.data_sync.service.ScriptPlanService;
import com.weds.data_sync.util.JsonData;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 执行方案 前端控制器
 * </p>
 *
 * @author lhw
 * @since 2023-12-29
 */
@Tag(name = "执行计划接口")
@RestController
@RequestMapping("/api/plan/v1")
public class ScriptPlanController {

    @Autowired
    private ScriptPlanService planService;

    @Operation(summary = "增加")
    @PostMapping("/add")
    public Integer add(@RequestBody @Validated ScriptPlanDto scriptPlanDto) {
        return planService.add(scriptPlanDto);
    }

    @Operation(summary = "删除")
    @PostMapping("/del")
    public Integer del(@RequestBody ScriptPlanDto scriptPlanDto) {
        return planService.del(scriptPlanDto.getIds());
    }

    @Operation(summary = "修改")
    @PostMapping("/update")
    public Integer update(@RequestBody ScriptPlanDto scriptPlanDto) {
        return planService.update(scriptPlanDto);
    }

    @Operation(summary = "查询")
    @PostMapping("/query")
    public JsonData.PageData<DataSyncScriptPlanDO> query(@RequestBody PageDto pageDto) {
        return planService.queryData(pageDto);
    }

    @Operation(summary = "执行计划")
    @Parameters({
            @Parameter(name = "planNo", description = "执行计划编号", required = true, in = ParameterIn.QUERY)
    })
    @GetMapping("/start")
    public JsonData startPlan(String planNo) {
        return planService.startPlan(planNo);
    }

    @Operation(summary = "停止计划")
    @Parameters({
            @Parameter(name = "planNo", description = "执行计划编号", required = true, in = ParameterIn.QUERY)
    })
    @GetMapping("/stop")
    public JsonData stopPlan(String planNo) {
        return planService.stopPlan(planNo);
    }

    @Operation(summary = "手动执行计划【同步】")
    @Parameters({
            @Parameter(name = "planNo", description = "执行计划编号", required = true, in = ParameterIn.QUERY)
    })
    @GetMapping("/execute")
    public JsonData execute(String planNo) {
        return planService.execute(planNo, false);
    }

    @Operation(summary = "手动执行计划【异步】")
    @Parameters({
            @Parameter(name = "planNo", description = "执行计划编号", required = true, in = ParameterIn.QUERY)
    })
    @GetMapping("/execute_async")
    public JsonData executeAsync(String planNo) {
        return planService.execute(planNo, true);
    }

}
